package net.shao.activity;

import android.content.Context;
import android.hardware.input.InputManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyphenate.chat.EMCallManager;
import com.socks.library.KLog;

import net.shao.R;
import net.shao.base.BaseActivity;
import net.shao.model.ChatListModel;
import net.shao.widget.HeadBar;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

public class ChatActivity extends BaseActivity {

    @ViewInject(R.id.head_chat)
    private HeadBar headBar;
    @ViewInject(R.id.listview_chat)
    private ListView listView;
    @ViewInject(R.id.rln_chatLayout)
    private RelativeLayout rln_chatLayout;

    @ViewInject(R.id.tv_yuying_chat)
    private TextView tv_yuying_chat;
    @ViewInject(R.id.et_chat)
    private EditText et_chat;
    @ViewInject(R.id.tv_image_chat)
    private TextView tv_image_chat;
    @ViewInject(R.id.tv_gengduo_chat)
    private TextView tv_gengduo_chat;
    @ViewInject(R.id.btn_commit_chat)
    private Button btn_commit_chat;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_chat);
        x.view().inject(this);
        ChatListModel model = (ChatListModel) getIntent().getSerializableExtra("chatListModel");
        KLog.i("model.toString>>>" + model.toString());
        headBar.setTitle(model.getChatTitle());
        headBar.setLeftText("返回");
        headBar.setOnLeftClickListener(view -> {
            finish();
        });

        et_chat.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    tv_gengduo_chat.setVisibility(View.GONE);
                    btn_commit_chat.setVisibility(View.VISIBLE);
                } else {
                    tv_gengduo_chat.setVisibility(View.VISIBLE);
                    btn_commit_chat.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        rln_chatLayout.setOnTouchListener((v, event) -> {
            hideInput(rln_chatLayout);
            return false;
        });

        listView.setOnTouchListener((v, event) -> {
            hideInput(listView);
            return false;
        });

    }

    private void hideInput(View view) {
        InputMethodManager inputMethodManager = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


}
